package com.hemaapp.yjnh.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.ImageDownloader;

import xtom.frame.util.XtomSharedPreferencesUtil;

/**
 * 类描述：图片加载工具类
 * 创建人：GuiyouZhang
 * 创建时间：2016/8/8 15:26
 * 修改备注：
 */
public class ImageUtils {

    /** 加载大图
     * @param context
     * @param url
     * @param imageView
     */
//    public static void loadBigImage(Context context, String url, ImageView imageView, int width, int height) {
//        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
//        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
//            return;
//        Glide.with(context.getApplicationContext()).load(url).placeholder(R.drawable.no_pic_big).error(R.drawable.no_pic_big)
//                .override(width, height).crossFade().into(imageView);
//    }

    /** 加载大图2，不对图片进行裁剪
     * @param context
     * @param url
     * @param imageView
     */
//    public static void loadBigImageInside(Context context, String url, ImageView imageView) {
//        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
//        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
//            return;
//        Glide.with(context.getApplicationContext()).load(url).placeholder(R.drawable.no_pic_big).error(R.drawable.no_pic_big)
//                .centerCrop().crossFade().into(imageView);
//    }

    public final static DisplayImageOptions avatarImageOptions  = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.default_rabbit_grey)
            .showImageOnFail(R.drawable.default_rabbit_grey)
            .showImageForEmptyUri(R.drawable.default_rabbit_grey)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .displayer(new CircleDisplayer())
            .build();

    public final static DisplayImageOptions smallImageOptions  = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.no_pic_small)
            .showImageOnFail(R.drawable.no_pic_small)
            .showImageForEmptyUri(R.drawable.no_pic_small)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    public final static DisplayImageOptions midImageOptions  = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.no_pic_mid)
            .showImageOnFail(R.drawable.no_pic_mid)
            .showImageForEmptyUri(R.drawable.no_pic_mid)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    public final static DisplayImageOptions bigImageOptions  = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.no_pic_big)
            .showImageOnFail(R.drawable.no_pic_big)
            .showImageForEmptyUri(R.drawable.no_pic_big)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    /**
     * 从drawable中异步加载本地图片
     *
     * @param imageId
     * @param imageView
     */
    public static void displayFromDrawable(int imageId, ImageView imageView) {
        ImageLoader.getInstance().displayImage("drawable://" + imageId,
                imageView);
    }

    /** 加载头像
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadAvatar(final Context context, String url, final ImageView imageView) {
        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
            return;
        ImageLoader.getInstance().displayImage(url, imageView, avatarImageOptions);
    }

    /** 加载头像，可以设置默认头像
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadAvatarWithDefault(final Context context, String url, final ImageView imageView, int defaultId) {
        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
            return;
        ImageLoader.getInstance().displayImage(url, imageView, new DisplayImageOptions.Builder()
                .showImageOnLoading(defaultId)
                .showImageOnFail(defaultId)
                .showImageForEmptyUri(defaultId)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new CircleDisplayer())
                .build());
    }

    /** 加载小图
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadSmalImage(Context context, String url, ImageView imageView) {
        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
            return;
        ImageLoader.getInstance().displayImage(url, imageView, smallImageOptions);
    }

    /** 加载大图
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadBigImage(Context context, String url, ImageView imageView) {
        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
            return;
        ImageLoader.getInstance().displayImage(url, imageView, bigImageOptions);
    }

    /** 加载分类
     * 默认分类是墨绿色
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadAssort(Context context, String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url, imageView, new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.all_sort)
                .showImageOnFail(R.mipmap.all_sort)
                .showImageForEmptyUri(R.mipmap.all_sort)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build());
    }

    /** 加载分类2
     * 默认分类是白色
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadAssortWhite(Context context, String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url, imageView, new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.default_sort)
                .showImageOnFail(R.drawable.default_sort)
                .showImageForEmptyUri(R.drawable.default_sort)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build());
    }

    /**加载圆角小图
     * @param context
     * @param url
     * @param imageView
     * @param radius
     */
    public static void loadRadiusImage(final Context context, String url,
                                       final ImageView imageView, final int radius) {
        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
            return;
//        ImageLoader.getInstance().displayImage(url, imageView, new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.drawable.default_radius_img_small)
//                .showImageForEmptyUri(R.drawable.default_radius_img_small)
//                .showImageOnFail(R.drawable.default_radius_img_small)
//                .cacheInMemory(true)
//                .cacheOnDisc(true)
//                .bitmapConfig(Bitmap.Config.RGB_565)   //设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(radius))
//                .build());
        Glide.with(context.getApplicationContext()).load(url).asBitmap().placeholder(R.drawable.default_radius_img_small)
                .error(R.drawable.default_radius_img_small).centerCrop()
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(BaseUtil.dip2px(context, radius));
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });

    }

    /**圆角大图
     * @param context
     * @param url
     * @param imageView
     * @param radius
     */
    public static void loadRadiusImageMid(final Context context, String url,
                                          final ImageView imageView, final int radius) {
        String onlyWifi = XtomSharedPreferencesUtil.get(context, "imageload_onlywifi");
        if (!TextUtils.isEmpty(onlyWifi) && onlyWifi.equals("true") && !NetUtils.isWifiConnect(context))
            return;
//        ImageLoader.getInstance().displayImage(url, imageView, new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.drawable.default_radius_img_mid)
//                .showImageForEmptyUri(R.drawable.default_radius_img_mid)
//                .showImageOnFail(R.drawable.default_radius_img_mid)
//                .cacheInMemory(true)
//                .cacheOnDisc(true)
//                .bitmapConfig(Bitmap.Config.RGB_565)   //设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(radius))
//                .build());
        Glide.with(context.getApplicationContext()).load(url).asBitmap().placeholder(R.drawable.default_radius_img_mid)
                .error(R.drawable.default_radius_img_mid).centerCrop()
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(BaseUtil.dip2px(context, radius));
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }

    /** 加载手机本地图片
     * @param context
     * @param path
     * @param imageView
     */
    public static void loadLocalImage(Context context, String path, ImageView imageView) {
        String imageUrl = ImageDownloader.Scheme.FILE.wrap(path);
        ImageLoader.getInstance().displayImage(imageUrl, imageView, smallImageOptions);
    }
}
